package com.dl.service.impl;

import com.dl.dao.FileMapper;
import com.dl.pojo.MyFile;
import com.dl.pojo.Page;
import com.dl.pojo.vo.MyFileVo;
import com.dl.service.FileService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author DL
 * @create 2021-04-11 9:50
 */
@Service
@Transactional
public class FileServiceImpl implements FileService {

    @Resource
    private FileMapper fileMapper;

    @Override
    @Transactional(readOnly = true)
    public int getTotal() {
        return fileMapper.getTotal();
    }

    @Override
    public boolean addFile(MyFile myFile) {
        return fileMapper.addFile(myFile) == 1;
    }

    @Override
    public boolean deleteFile(int id) {

        return fileMapper.deleteFile(id) == 1;
    }

    @Override
    public boolean updateFile(MyFile myFile) {
        return fileMapper.updateFile(myFile) == 1;
    }

    @Override
    @Transactional(readOnly = true)
    public MyFile queryFileById(int id) {
        return fileMapper.queryFileById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<MyFile> queryAllFile() {
        return fileMapper.queryAllFile();
    }

    @Override
    @Transactional(readOnly = true)
    public Map<Integer, Integer> queryGroupBy() {
        return fileMapper.queryGroupBy();
    }

    @Override
    @Transactional(readOnly = true)
    public Page<MyFile> queryFileByLimit(int currentPage, int pageSize) {
        //map集合，保存分页基本信息
        Map<String, Integer> map = new HashMap<>();
        //分页对象，保存分页所有信息
        Page<MyFile> page = new Page<>();
        //设置数据总数
        page.setTotal(fileMapper.getTotal());
        //设置页面大小，同时计算页面总数
        page.setPageSize(pageSize);

        map.put("currentPage", (currentPage-1)*pageSize);
        map.put("pageSize", pageSize);

        //查询到的学生集合放入page对象中
        List<MyFile> files = fileMapper.queryFileByLimit(map);
        page.setList(files);
        return page;
    }

    @Override
    @Transactional(readOnly = true)
    public Page<MyFile> queryFileByKey(String keyWord, int currentPage, int pageSize) {
        //分页对象，保存分页所有信息
        Page<MyFile> page = new Page<>();

        //map集合，保存分页基本信息
        Map<String, Object> map = new HashMap<>();
        map.put("keyWord", keyWord);
        map.put("currentPage", (currentPage-1)*pageSize);
        map.put("pageSize", pageSize);

        List<MyFile> myFiles = fileMapper.queryFileByKey(map);
        page.setList(myFiles);

        //设置数据总数
        page.setTotal(myFiles.size());
        //设置页面大小，同时计算页面总数
        page.setPageSize(pageSize);

        return page;
    }

    @Override
    @Transactional(readOnly = true)
    public Page<MyFileVo> queryFileVoByLimit(Integer tid, int currentPage, int pageSize) {
        //map集合，保存分页基本信息
        Map<String, Integer> map = new HashMap<>();
        //分页对象，保存分页所有信息
        Page<MyFileVo> page = new Page<>();

        Integer totalByTID = fileMapper.getTotalByTID(tid);
        System.out.println("totalByTID = " + totalByTID);
        if (totalByTID != 0){
            //设置数据总数
            page.setTotal(totalByTID);
            //设置页面大小，同时计算页面总数
            page.setPageSize(pageSize);
            map.put("tid", tid);
            map.put("currentPage", (currentPage-1)*pageSize);
            map.put("pageSize", pageSize);
            //查询到的学生集合放入page对象中
            List<MyFileVo> files = fileMapper.queryFileVoByLimit(map);
            page.setList(files);
            return page;
        }else {
            return null;
        }
    }
}
